#include<vector>
#include<unordered_map>
#include<string>
#include<iostream>
using namespace std;

vector<int> findSubstring(string s, vector<string>& words) {
    int len = words[0].size(), m = s.size(), n = words.size();
    vector<int> ret;
    int mul = len * n;
    unordered_map<string, int> hash1;
    for (auto& x : words) hash1[x]++;
    for (int i = 0; i < len; i++)
    {
        unordered_map<string, int> hash2;
        for (int left = i, right = i; right + len <= m; right += len)
        {
            string in = s.substr(right, len);
            while (right - left + 1 > mul)
            {
                string out = s.substr(left, len);
                if (hash1 == hash2)
                    ret.push_back(left);
                hash2[out]--;
                if (hash2[out] == 0)
                    hash2.erase(out);
                left += len;
            }
            hash2[in]++;
            while (right - left + 1 > mul)
            {
                string out = s.substr(left, len);
                if (hash1 == hash2)
                    ret.push_back(left);
                hash2[out]--;
                if (hash2[out] == 0)
                    hash2.erase(out);
                left += len;
            }
        }
    }
    return ret;
}

int main()
{
	/*unordered_map<int, int> hash1, hash2;
	hash1[3]++;
	hash1[1];
	hash2[3]++;
	if (hash1 == hash2)
		cout << 1;*/
    vector<string> v{ "word","good","best","good" };
    findSubstring("wordgoodgoodgoodbestword", v);

	return 0;
}